package com.xbs.dao.enterprise;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.EpOptionEitity;
import com.xbs.entity.EpOptionOrderQueryEntity;
import com.xbs.utils.EnterpriseUtils;

public class EpOptionOrderDao extends BaseDao{
	/**
	 * 获取今天企业的车组订单
	 */
	EnterpriseUtils util=new EnterpriseUtils();
	public List<EpOptionEitity> getTodayOptionOrder(){
		String today=util.getSimpleDate();
		String sql="select o.eo_order_id, o.eo_account,o.eo_reserve_get_time,o.eo_fact_return_time,o.eo_moto_perday_rent,o.eo_order_status,o.eo_rent_type,m.rent_money,m.car_model_number"+
		" from tb_enterprise_model m,tb_enterprise_order o"+
		" where m.eo_order_id=o.eo_order_id and o.eo_rent_type=1 and DATE_FORMAT(o.eo_establish_time,'%Y-%m-%d')=? group by o.eo_order_id";
		
		RowDatas<EpOptionEitity> rd=new RowDatas<EpOptionEitity>() {

			@Override
			public EpOptionEitity getRowData(ResultSet rs) throws SQLException {
				EpOptionEitity ep=new  EpOptionEitity();
				ep.setEo_order_id(rs.getString("eo_order_id"));
				ep.setEo_account(rs.getString("eo_account"));
				ep.setEo_reserve_get_time(rs.getDate("eo_reserve_get_time"));
				ep.setEo_fact_return_time(rs.getDate("eo_fact_return_time"));
				ep.setPerdayCash(rs.getDouble("eo_moto_perday_rent"));
				ep.setEo_order_status(rs.getString("eo_order_status"));
				ep.setEo_rent_type(rs.getInt("eo_rent_type"));
				return ep;
			}
			
		};
		System.out.println(today);
		return queryList(sql, new ParamSet(today),rd);
		
	}
	/**
	 * 根据条件查询车队自选订单
	 */
	public List<EpOptionEitity> getEpOrderOption(EpOptionOrderQueryEntity query,int curr,int size){
		String sql="select o.eo_order_id, o.eo_account,o.eo_reserve_get_time,o.eo_fact_return_time,o.eo_moto_perday_rent,o.eo_order_status,o.eo_rent_type,m.rent_money,m.car_model_number from tb_enterprise_model m,tb_enterprise_order o "+
				"where m.eo_order_id=o.eo_order_id and o.eo_rent_type=1 ";	
		StringBuffer bf=new StringBuffer(sql);
		if(query.getStartTime()!=null){
			bf.append("and DATE_FORMAT(o.eo_establish_time,'%Y-%m-%d')>'"+query.getStartTime()+"'");
		}
		if(query.getEndTime()!=null){
			bf.append(" and DATE_FORMAT(o.eo_establish_time,'%Y-%m-%d')>'"+query.getEndTime()+"'");
		}
		if(!query.getStatus().equals("全部")){
			bf.append(" and o.eo_order_status='"+query.getStatus()+"'");
		}
		bf.append(" group by o.eo_order_id limit ").append((curr-1)*size).append(",").append(size);
		RowDatas<EpOptionEitity> rd=new RowDatas<EpOptionEitity>() {
			@Override
			public EpOptionEitity getRowData(ResultSet rs) throws SQLException {
				EpOptionEitity ep=new  EpOptionEitity();
				ep.setEo_order_id(rs.getString("eo_order_id"));
				ep.setEo_account(rs.getString("eo_account"));
				ep.setEo_reserve_get_time(rs.getDate("eo_reserve_get_time"));
				ep.setEo_fact_return_time(rs.getDate("eo_fact_return_time"));
				ep.setPerdayCash(rs.getDouble("eo_moto_perday_rent"));
				ep.setEo_order_status(rs.getString("eo_order_status"));
				ep.setEo_rent_type(rs.getInt("eo_rent_type"));
				return ep;
			}
		};
		return queryList(bf.toString(), rd);
	}
	/**
	 * 返回条件的总记录数
	 */
	public Integer getSumOrderQuery(EpOptionOrderQueryEntity query){
		String sql="select count(*) sum from tb_enterprise_model m,tb_enterprise_order o "+
				"where m.eo_order_id=o.eo_order_id and o.eo_rent_type=1 ";	
		StringBuffer bf=new StringBuffer(sql);
		if(query.getStartTime()!=null){
			bf.append("and DATE_FORMAT(o.eo_establish_time,'%Y-%m-%d')>'"+query.getStartTime()+"'");
		}
		if(query.getEndTime()!=null){
			bf.append(" and DATE_FORMAT(o.eo_establish_time,'%Y-%m-%d')>'"+query.getEndTime()+"'");
		}
		if(!query.getStatus().equals("全部")){
			bf.append(" and o.eo_order_status='"+query.getStatus()+"'");
		}
		bf.append(" group by o.eo_order_id");
		RowDatas<Integer> rd=new RowDatas<Integer>() {
			@Override
			public Integer getRowData(ResultSet rs) throws SQLException {
				return rs.getInt("sum");
			}
		};
		return query(bf.toString(), rd);
	}
	
} 
